package com.jr.dao.impl;

import com.jr.dao.IProjectInfoDao;
import com.jr.pojo.ProjectInfo;
import com.jr.util.DBHelper;
import com.jr.util.PageHelper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class ProjectInfoDaoImpl implements IProjectInfoDao {

    DBHelper db = new DBHelper();

    @Override //根据项目编号 查询单条项目信息
    public ArrayList<ProjectInfo> selectByPid(Integer pid) {
        String sql="select * from projectinfo where pid=?";
        return db.dtQuery(sql,ProjectInfo.class,pid);
    }

    @Override //修改项目信息
    public int updateProject(ProjectInfo pro) {
        String sql="update projectinfo set status=? where pid=?";
        Object[] obj= {pro.getStatus(),pro.getPid()};
        return db.dtUpdate(sql,obj);
    }


    @Override //1.分页 查询总条数：
    public int selectDataCount(Integer status) {
        StringBuilder sql =new StringBuilder("select count(pid) from projectinfo where 1=1");
        if(status>=0 && status<=2){
            sql.append(" and status="+status);
        }
        Connection con =null;
        PreparedStatement ps = null;
        ResultSet resultSet = null;
        try {
            con = db.getCon();
            ps = con.prepareStatement(sql.toString());
            resultSet = ps.executeQuery();
            if (resultSet.next()) {
                return resultSet.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            db.closeAll(con,ps,resultSet);
        }
        return 0;
    }

    @Override //2.分页 根据条件查询 当前页 的数据
    public ArrayList<ProjectInfo> selectByStatus(Integer status, PageHelper ph) {
        StringBuilder sql = new StringBuilder("select * from projectinfo where 1=1"); //全查
        if(status>=0 && status<=2){
            sql.append(" and status="+status);
        }
        sql.append(" limit ?,?");
        int index = (ph.getPageIndex()-1)*ph.getShowNumber();

        return db.dtQuery(sql.toString(), ProjectInfo.class,index,ph.getShowNumber());
    }
}
